ZKP_ITEM_HEIGHT = 16;
ZKP_ITEMS_SHOWN = 12;

zLootList_searchResults = {};

zLootList_Colors = {
	["ff8000"] = 5,
	["a335ee"] = 4,
	["0070dd"] = 3, 
	["1eff00"] = 2,
	["ffffff"] = 1,
	["9d9d9d"] = 0,
			
	[-1] = "fff68f",
	[0] = "9d9d9d", 
	[1] = "ffffff",
	[2] = "1eff00", 
	[3] = "0070dd",
	[4] = "a335ee",
	[5] = "ff8000",};

function zLootList_GetLink(id)

	local name, link, quality, _, _, _, _, _, _, _ = GetItemInfo("item:"..id..":0:0:0");
	local color	= zLootList_Colors[quality];
	
	if (not	color or not link or not name) then
		return nil;
	end

--	local textLink = "|cff" .. color .. "|H" .. link .. "|h[" .. name.. "]|h|r";

--	return textLink;
	return link;
end

 function zLootListScrollBar_Update()
   local line; -- 1 through 5 of our window to scroll
   local lineplusoffset; -- an index into our data calculated from the scroll offset

   local zTmpDate = nil;



   FauxScrollFrame_Update(zLootListScrollBar,getn(zLootList_searchResults),ZKP_ITEMS_SHOWN,ZKP_ITEM_HEIGHT);
   for line=1,ZKP_ITEMS_SHOWN do
     lineplusoffset = line + FauxScrollFrame_GetOffset(zLootListScrollBar);
     if lineplusoffset <= getn(zLootList_searchResults) then

	     local _, _, dTime, dNick, dDKP, _, dID,dItemName,dZone = string.find(zLootList_searchResults[lineplusoffset], "(%d+)@(.+)@(%d+)@(.+)@(%d+)@(.+)@(.+)");


			zTmpDate = nil;

		     	zTmpDate = date("*t", dTime);
			if(zTmpDate["hour"]<=9) then zTmpDate["hour"] = "0"..zTmpDate["hour"]; end
			if(zTmpDate["min"]<=9) then zTmpDate["min"] = "0"..zTmpDate["min"]; end

			local dTimeNew = format("%s:%s %d/%d", zTmpDate["hour"],zTmpDate["min"],zTmpDate["day"],zTmpDate["month"]); --fix date to a more searchable variable


		if(zLootList_GetLink(dID)==nil) then
		       getglobal("ItemEntry"..line.."_Item"):SetText(dItemName);
		else
		       getglobal("ItemEntry"..line.."_Item"):SetText(zLootList_GetLink(dID));
		end
		       getglobal("ItemEntry"..line.."_Nick"):SetText(dNick);
		       getglobal("ItemEntry"..line.."_DKP"):SetText(dDKP);
		       getglobal("ItemEntry"..line.."_Date"):SetText(dTimeNew);
		       getglobal("ItemEntry"..line.."_LinuxDate"):SetText(dTime);
		       getglobal("ItemEntry"..line):Show();
     else
       getglobal("ItemEntry"..line):Hide();
     end

   end
 end


function zLootList_GetIDname(id)
if(id==nil) then
return "";
end

	local name, _, _, _, _, _, _, _, _, _ = GetItemInfo("item:"..id..":0:0:0");
	return name;
end

function zLootListFrame_Search()


local searchStr = zLootListFrame_SearchTextbox:GetText();
searchStr = string.gsub(searchStr, "*", ".+");

local zTmpDate = nil;
local zSpecialTmpDate = nil;

zLootList_searchResults = {};

	for i=1, table.getn(BidHistory), 1 do

		local _, _, dTime, dNick, dID, dItemName, dDKP, dZone = string.find(BidHistory[i], "(%d+)@(.+)@(%d+)@(.+)@(%d+)@(.+)");

			if (not	dNick or not dID or not dDKP or not dTime) then
				return nil;
			end
		dItem = zLootList_GetIDname(dID);

		if(dItem==nil) then
		dItem = dItemName;
		end

		zTmpDate = nil;

		zTmpDate = date("*t", dTime);
		if(zTmpDate["hour"]<=9) then zTmpDate["hour"] = "0"..zTmpDate["hour"]; end
		if(zTmpDate["min"]<=9) then zTmpDate["min"] = "0"..zTmpDate["min"]; end

		local dTimeNew = format("%s:%s %d/%d", zTmpDate["hour"],zTmpDate["min"],zTmpDate["day"],zTmpDate["month"]); --fix date to a more searchable variable

		zSpecialTmpDate = nil;
		local zSpecialTmpDate = date("*t", dTime);
		local zSpecialTimeNew = format("%d/%d", zSpecialTmpDate["day"],zSpecialTmpDate["month"]); --fix date to a more searchable variable


		if (string.find(string.lower(dItem.."player:"..dNick..dID..dDKP..dTimeNew.."date:"..zSpecialTimeNew..dZone), string.lower(searchStr))) then


			table.insert(zLootList_searchResults,dTime.."@"..dNick.."@"..dDKP.."@"..dTimeNew.."@"..dID.."@"..dItem.."@"..dZone); --sort by item-name

		end

	end

if(getn(zLootList_searchResults)==0) then
	zLootList_NoMatches:Show();
else
	zLootList_NoMatches:Hide();
end

sort(zLootList_searchResults,function(a,b) return b < a end);
zLootListScrollBar_Update();

end